home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 603 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.7 KB

  1. From: Marco_Dalla-Gasperina@vcd.hp.com (Marco Dalla Gasperina)
  2. Message-ID: <4h5502$6gs@news.vcd.hp.com>
  3. X-Original-Date: Thu, 29 Feb 96 21:18:25 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 01 Mar 96 14:06:12 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: Generic Object Callbacks
  9. Organization: HP
  10. References: <pgpmoose.199602221531.14635@isolde.mti.sgi.com> <4gsi2p$905@bcarh8ab.bnr.ca> <4guh9e$jt6@sdaw04.seinf.abb.se> <4h1pb6$n19@news2.cais.com> <3135C73D.5570@acf4.nyu.edu>
  11. X-Newsreader: News Xpress Version 1.0 Beta #4
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMTcEkOEDnX0m9pzZAQGUjwF/d7vYT3hypL1RfNBmnAyE9ERQeYS5BjKX
  14.     s8DBP0+FxwYejp6T4BCXCYw+0aLLHCNi
  15.     =91kC
  16.  
  17. In article <3135C73D.5570@acf4.nyu.edu>, Shalom Reich <sqr1874@acf4.nyu.edu> 
  18. wrote:
  19. >Ben Jones wrote:
  20. >> 
  21. >> Anders Lindback (alindbac@sw.seisy.abb.se) wrote:
  22. >> : In article <4gsi2p$905@bcarh8ab.bnr.ca>,
  23. >> : brian (b.c.) white <bcwhite@bnr.ca> wrote:
  24. >> : >
  25. >> : >>What is needed is the ability to use member functions as callbacks
  26. >> : >>without any constraint on the type of the object they are invoked on.
  27. >> : >>This is possible in C++ only by using various implementation-dependent
  28. >> : >>hacks to escape the type system, because the language does not provide
  29. >> : >>any way to express such a construct.
  30. >> : >
  31.  
  32. You really don't need hacks at all.  It is completely possible to do it
  33. with the way the language is defined.  See below.
  34.  
  35. >I don't seem to understand the discussion in this thread.  Yet it seems 
  36. >to generate some strong opinions.  So, . . . 
  37. >
  38. >The routine that will issue the callback (let us call it the driver) 
  39. >needs to use a stored pointer.  The argument is that the stored pointer
  40. >should be to a member function.  
  41. >Why isn't it good enough for the stored pointer to be a pointer to the 
  42. >object and then the driver will only need to say "object->callback();" in
  43. >order to call the proper routine.
  44.  
  45. The problem here is probably that you are constraining all "callbackable"
  46. objects to be derived from a common base class which may not be a natural
  47. attribute of the system.
  48.  
  49. >The usual virtual function semantics would take care of any inheritance
  50. >hierarchy.
  51.  
  52. Here's one way to do it (excuse any syntax errors as I haven't compiled
  53. this) ...
  54.  
  55. class CallbackBase {
  56.     public:
  57.     virtual doit();
  58. };
  59.  
  60. template<class T> class Callback : public CallbackBase {
  61.     private:
  62.     // the callback object saves a pointer to a member function
  63.     // and a pointer to the target object.
  64.     void (T::*callbackFunc)();
  65.     T* target;
  66.  
  67.     public:
  68.     // save the pointer-to-member and target object
  69.     Callback( T* obj, void (T::*cbf)() ) : target(obj), callbackFunc(cbf) {}
  70.     // doit issues the callback
  71.     void doit() { target->*callbackFunc)(); }
  72. };
  73.  
  74. Heres the driver code...
  75. DriverFunc( CallbackBase* cbb )
  76. {
  77.     ... 
  78.     cbb->doit();
  79.     ...
  80.     delete cbb; 
  81. }
  82.  
  83. Heres the client code:
  84.  
  85. class MyCleck {
  86.     void Cleck() { ... }
  87. };
  88.  
  89. class MyBlurp {
  90.     void Blurp() { ... }
  91. };
  92.  
  93. main()
  94. {
  95.     ...
  96.     MyCleck   mc;
  97.     MyBlurp   mb;
  98.  
  99.     DriverFunc( new Callback<MyCleck>(&mc,&MyCleck::Cleck) );
  100.     DriverFunc( new Callback<MyBlurp>(&mb,&MyBlurp::Blurp) );
  101.     ...
  102. }
  103.  
  104. I think this should work, as I have implemented the publisher/
  105. subscriber (observer/observed) pattern in an almost identical
  106. way.
  107.  
  108. The problem is a little trickier trying to generalize any
  109. parameters that the callback methods require and still keep
  110. them typesafe.
  111.  
  112. >Shalom Reich
  113.  
  114. marco
  115. ---
  116. [ To submit articles: try just posting with your news-reader.
  117.                       If that fails, use mailto:std-c++@ncar.ucar.edu
  118.   FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  119.   Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  120.   Comments? mailto:std-c++-request@ncar.ucar.edu.
  121. ]
  122.